﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFMonad
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
   

    public interface ISayHello
    {
        Unit SayHello(string Name);
        string GetName();
    }
    
    public partial class Window1 : Window, ISayHello
    {
        
        public Window1()
        {
            InitializeComponent();
            View<ISayHello> view = this.AsView<Window1, ISayHello>();
            

            string result = (from v in view
                          select v.GetName()).Value();

            SayHello.Click += (e, args) =>
                                  {
                                      ThreadStart ts = () => (from v in view
                                                              let name = v.GetName()
                                                              select v.SayHello(name)).Do();

                                      ts.BeginInvoke(ts.EndInvoke, null);
                                  };
                
        }
        Unit ISayHello.SayHello(string name)
        {
            Greeting.Text = name;
            return Unit.Unit;
        }
        string ISayHello.GetName()
        {
            return Name.Text;
        }
    }
}
